package service

import (
	"fmt"
	"gorm.io/gorm"
	"mysql_study/models"
	"mysql_study/utils"
	"time"
)

func UserOpt() {
	//EditUserBasic()
	//AddUserList() // 插入多个数据
	UpdateUserListWhere()
}

// AddUser 操作 `user_basic` 表
func AddUser() {
	fmt.Println("AddUser 操作user表")
	// 单个新增数据到 user 表
	item := models.UserBasic{
		Model:         gorm.Model{},
		UserName:      utils.GetRandName(3, true),
		Password:      utils.GetRandName(8, false),
		Phone:         utils.GetRandName(11, false),
		Email:         utils.GetRandName(8, false),
		Identity:      utils.GetRandName(3, false),
		ClientIP:      utils.GetRandName(6, false),
		ClientPort:    utils.GetRandName(4, false),
		LoginTime:     time.Now(),
		HeartbeatTime: time.Now(),
		LogOutTime:    time.Now(),
		IsLogout:      false,
	}

	utils.MySqlDB.Create(&item)
	fmt.Println(item.ID)
}

// EditUserBasic 编辑 `user_basic` 表信息
func EditUserBasic() {
	item := models.UserBasic{
		Model:         gorm.Model{},
		ID:            7,
		UserName:      utils.GetRandName(3, true),
		Password:      utils.GetRandName(8, false),
		Phone:         utils.GetRandName(11, false),
		Email:         utils.GetRandName(8, false),
		Identity:      utils.GetRandName(3, false),
		ClientIP:      utils.GetRandName(6, false),
		ClientPort:    utils.GetRandName(4, false),
		LoginTime:     time.Now(),
		HeartbeatTime: time.Now(),
		LogOutTime:    time.Now(),
		IsLogout:      false,
	}
	kk := utils.MySqlDB.Save(&item)
	fmt.Println(kk.Error)
}

// AddUserList 插入多个数据
func AddUserList() {
	var data []*models.UserBasic
	for i := 0; i < 100; i++ {
		item := models.UserBasic{
			Model:         gorm.Model{},
			UserName:      utils.GetRandName(3, true),
			Password:      utils.GetRandName(8, false),
			Phone:         utils.GetRandName(11, false),
			Email:         utils.GetRandName(8, false),
			Identity:      utils.GetRandName(3, false),
			ClientIP:      "999",
			ClientPort:    utils.GetRandName(4, false),
			LoginTime:     time.Now(),
			HeartbeatTime: time.Now(),
			LogOutTime:    time.Now(),
			IsLogout:      false,
		}
		data = append(data, &item)
	}
	res := utils.MySqlDB.Create(data)
	fmt.Println(res)
}

// UpdateUserListWhere 批量更新多个数据
func UpdateUserListWhere() {
	// 根据 ClientIP:      "999", 批量更新数据信息 is_logout = 3
	targeTmp := "999"
	res := utils.MySqlDB.Debug().Model(&models.UserBasic{}).Where(" client_ip = ?", targeTmp).UpdateColumn("is_logout", gorm.Expr(" 3 "))
	// UPDATE `user_basic` SET `is_logout`= 3  WHERE  client_ip = '999' AND `user_basic`.`deleted_at` IS NULL
	fmt.Println(res)

}

func hello() {
	utils.MySqlDB.Model(&models.UserBasic{}).Update("price", gorm.Expr("price * ? + ?", 2, 100))
	//// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';

	utils.MySqlDB.Model(&models.UserBasic{}).Updates(map[string]interface{}{"price": gorm.Expr("price * ? + ?", 2, 100)})
	//// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';

	utils.MySqlDB.Model(&models.UserBasic{}).UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))
	//// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2';

	utils.MySqlDB.Model(&models.UserBasic{}).Where("quantity > 1").UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))
	//// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2' AND quantity > 1;
}
